package com.wenlin.myproduction.widget;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


import com.wenlin.myproduction.util.ValCommon;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cwl
 * @desc 光圈
 * @Email 377979485@qq.com
 * @time 2023/8/9 16:59
 */
public class CircleLightView extends View {
    private static final int START_ALPHA = 120;
    private static final int CIRCLE_NUM = 1;//几个同心圆
    private float distanceDelta;//圆与圆之间的距离
    @ColorInt
    private final int colorFt = Color.parseColor("#FFFFFFFF");//圆的颜色
    private float valueFloat;//动画实时的值
    private Paint paint, paintCircle;
    private ValueAnimator animator;
    private float centerX, centerY;
    private List<Float> list;
    private float radius;
    private int alpha;
    private float strokeWidth;
    private boolean isChangeAlpha;
    private boolean autoStopAnim;//自动停止动画


    public CircleLightView(Context context) {
        this(context, null);
    }

    public CircleLightView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setAntiAlias(true);
        paint.setColor(colorFt);
        paint.setDither(true);
        paintCircle = new Paint();
        paintCircle.setStyle(Paint.Style.FILL_AND_STROKE);
        paintCircle.setStrokeCap(Paint.Cap.ROUND);
        paintCircle.setAntiAlias(true);
        paintCircle.setColor(colorFt);
        paintCircle.setDither(true);
        list = new ArrayList<>();
    }

    /**
     * 设置改变不透明度
     *
     * @param b 开关
     */
    public void setChangeAlpha(boolean b) {
        isChangeAlpha = b;
        invalidate();
    }

    public void autoStart() {
        autoStopAnim = false;
        if (animator == null || !animator.isRunning()) startAnim();
    }

    public void autoStop() {
        autoStopAnim = true;
    }

    /**
     * 开始动画
     */
    private void startAnim() {
        stopAnim();
        animator = ValueAnimator.ofFloat(0, 1);
        animator.addUpdateListener(valueAnimator -> {
            valueFloat = (float) valueAnimator.getAnimatedValue();
            invalidate();
        });
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(@NonNull Animator animator) {

            }

            @Override
            public void onAnimationEnd(@NonNull Animator animator) {

            }

            @Override
            public void onAnimationCancel(@NonNull Animator animator) {

            }

            @Override
            public void onAnimationRepeat(@NonNull Animator animator) {
                if (autoStopAnim) stopAnim();
            }
        });
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setRepeatMode(ValueAnimator.RESTART);
        animator.setDuration(ValCommon.ANIM_DURATION_OF_CIRCLE_LIGHT_VIEW * CIRCLE_NUM);
        animator.setInterpolator(new LinearInterpolator());
        animator.start();
    }

    /**
     * 停止动画
     */
    private void stopAnim() {
        if (animator != null) {
            animator.cancel();
            animator = null;
        }
    }

    /**
     * 初始化集合数据
     */
    private void initListData() {
        for (int i = 0; i < CIRCLE_NUM; i++) {
            list.add(distanceDelta * i + distanceDelta / 2f);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (centerX == 0 || centerY == 0) {
            centerX = getWidth() / 2f;
            centerY = getHeight() / 2f;
            distanceDelta = centerX / CIRCLE_NUM;
            paint.setStrokeWidth(centerX);
        }
        if (list.size() == 0) {
            initListData();
        }
        for (int i = 0; i < list.size(); i++) {
            Float ripple = list.get(i);
            radius = ripple + centerX * valueFloat;
            if (radius > centerX) radius -= centerX;
            strokeWidth = centerX / 2f - Math.abs(centerX / 2f - radius);
            paint.setStrokeWidth(strokeWidth);
            if (isChangeAlpha) {
                alpha = (int) ((centerX - radius) / centerX * START_ALPHA);
                paint.setAlpha(alpha);
            }
            if (radius > strokeWidth / 2f) canvas.drawCircle(centerX, centerY, radius, paint);
        }
        canvas.drawRoundRect(centerX - centerX / 2f, centerY - centerX / 2f, centerX + centerX / 2f, centerY + centerX / 2f, centerX / 2f, centerX / 2f, paintCircle);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stopAnim();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        startAnim();
    }
}
